package com.tanhua.manage.listener;

import com.tanhua.commons.properties.HuaWeiUGCProperties;
import com.tanhua.commons.template.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.mongo.PublishApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@RocketMQMessageListener(
        topic = "tanhua-publish-152",
        consumerGroup = "tanhua-publish-consumer152")
public class PublishMessageListener implements RocketMQListener<String>{

    @Reference
    private PublishApi publishApi;
    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String publishId) {
        // 根据动态id查询
        Publish publish = publishApi.findById(publishId);
        // 判断 (容器中旧数据可能导致publishId为空)
        if (publish != null && publish.getState() == 0) {
            // 获取动态文本内容
            String textContent = publish.getTextContent();
            // 调用华为云内容审核：文字审核
            boolean textResult = huaWeiUGCTemplate.textContentCheck(textContent);
            if (textResult) {
                // 文件内容审核通过，进行图片审核
                List<String> medias = publish.getMedias();
                // 调用华为云内容审核：图片审核
                boolean imgResult =
                        huaWeiUGCTemplate.imageContentCheck(medias.toArray(new String[]{}));
                // 审核状态，默认为2表示审核不通过
                Integer state = 2;
                if (imgResult) {
                    state = 1; // 审核通过
                }
                // 修改状态
                publishApi.updateById(publishId,state);
            }
        }
    }
}
